home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacGofer 0.22d / MacGofer Sources / mac_windows.c < prev    next >
Encoding:
Text File  |  1994-04-07  |  43.6 KB  |  348 lines  |  [TEXT/MPS ]

  1. ION_NUM_WINDOWS - positionIndex);
  2.  
  3.   /* Do the real work */
  4.   MoveWindow(window, windowR.left, windowR.top, false);
  5.   SizeWindow(window, windowR.right - windowR.left, windowR.bottom - windowR.top, false);
  6. }
  7.  
  8.  
  9. /*
  10.    Create a new window of the given resource type called fname.
  11.    Show it and add it to the window menu if requested.
  12. */
  13.  
  14. int CreateNewWindow(windowtype,fname,doshow,windowmenu,doposition,fittoviewrect)
  15. char *fname;
  16. Boolean doshow, windowmenu, doposition,fittoviewrect;
  17. {
  18.   int windex;
  19.   extern short currentfnum, currentfsize;
  20.   WindowPtr whichWindow;
  21.   
  22.   SetCursor(*watchcurs);
  23.   
  24.   windex = CreateMyWindow();
  25.   whichWindow = GetNewWindow(windowtype,NIL, (WindowPtr)-1);
  26.   WINDOW(windex) = whichWindow;
  27.  
  28.   /* Write into the new window */
  29.   SetPort(whichWindow);
  30.  
  31.   /* If requested, position the window neatly */
  32.   if (doposition)
  33.     PositionNewWindow(whichWindow, windex - NUM_SYS_WINDOWS);
  34.  
  35.   /* If the window can be zoomed, set up its zoom rectangles */
  36.   if ( ((WindowPeek)whichWindow)->spareFlag )
  37.     {
  38.       WStateDataHandle stateDataH = (WStateDataHandle)((WindowPeek)whichWindow)->dataHandle;
  39.  
  40.       (**stateDataH).stdState = qd.screenBits.bounds;
  41.       (**stateDataH).stdState.top += 38; /* Note: this should probably be set dynamically */
  42.       InsetRect(&(**stateDataH).stdState, POSITION_SCREEN_INSET, POSITION_SCREEN_INSET);
  43.  
  44.       (**stateDataH).userState = whichWindow->portRect;
  45.       LocalToGlobalRect(&(**stateDataH).userState);
  46.     }
  47.  
  48.   /* Set up text characteristics */
  49.   TextFont(currentfnum);
  50.   TextSize(currentfsize);
  51.  
  52.   /* Open a TE Window  */
  53.   NewTERec(windex,fittoviewrect);
  54.  
  55.   /*  Horizontal scroll bar */
  56.   HSCROLL(windex) = GetNewControl( Res_HScroll_bar, whichWindow);
  57.   
  58.   /*  Vertical scroll bar */
  59.   VSCROLL(windex) = GetNewControl( Res_VScroll_bar, whichWindow);
  60.  
  61.   /* Size the scroll bars */
  62.   ScrollHControl(HSCROLL(windex),whichWindow);
  63.   ScrollVControl(VSCROLL(windex),whichWindow);
  64.  
  65.  
  66.   /* Set the window title and reference */
  67.   setwtitle(whichWindow,fname);
  68.   FILENAME(windex) = safemalloc(strlen(fname)+1);
  69.   strcpy(FILENAME(windex),fname);
  70.   SetWRefCon(whichWindow,windex+1);
  71.  
  72.  
  73.   /* Add this window to the window menu if required */
  74.   if(windowmenu)
  75.     {
  76.       short mitems = CountMItems(Menu_Window);
  77.       char itembuf[256];
  78.       if(mitems < 10 + 3)
  79.         {
  80.           sprintf(itembuf,"/%c%s",mitems+'0'-3,fname);
  81.           insmenuitem(Menu_Window,itembuf,mitems);
  82.     }
  83.       else
  84.         insmenuitem(Menu_Window,fname,mitems);
  85.       setwindowtitle(windex,fname);
  86.     }
  87.  
  88.   /* Show the window if required */
  89.   if(doshow)
  90.     {
  91.       ShowWindow(whichWindow);
  92.       DrawGrowIcon(whichWindow);
  93.       DrawGoferIcons(windex);
  94.       DrawControls(whichWindow);
  95.       ValidRect(&whichWindow->portRect);
  96.     }      
  97.  
  98.   InitCursor();
  99.   return(windex);
  100. }
  101.  
  102.  
  103.  
  104. /*
  105.   Create a new untitled window.
  106. */
  107.  
  108.  
  109. newUntitledWindow()
  110. {
  111.   static int num_created = 0;
  112.   char file[20];
  113.   sprintf(file,"Untitled%d",++num_created);
  114.   (void) CreateNewWindow(Res_OpenWindow,file,TRUE,TRUE,AUTO_POSITION,FALSE);
  115. }
  116.  
  117.  
  118. /****************************************************************************
  119.  
  120.     Handling multiple windows with the same name.
  121.     
  122. ****************************************************************************/
  123.  
  124.  
  125. /*
  126.    Return the highest version of a file.
  127. */
  128.  
  129. findHighestVersion(title)
  130. char *title;
  131. {
  132.   int i;
  133.   int version = -1;
  134.  
  135.   for(i=0; i < NUM_WINDOWS; ++i)
  136.     if(OPEN(i))
  137.       {
  138.     if(equalfiles(title,FILENAME(i)))
  139.       setversion(i,++version);
  140.       }
  141.  
  142.   return(version);
  143. }
  144.  
  145.  
  146. /*
  147.    Reset all version numbers.  Don't add a version number
  148.    to the name if windex is ILLEGAL_WINDOW.
  149. */
  150.  
  151. resetversions(name,windex)
  152. char *name;
  153. int windex;
  154. {
  155.   char wtitle[256];
  156.   int i, version = 0;
  157.   char num[5];
  158.   short menuitem;
  159.   Boolean simpletitle = windex == ILLEGAL_WINDOW && 
  160.                         findHighestVersion(name) <= 0;
  161.  
  162.   
  163.   for(i=0; i < NUM_WINDOWS; ++i)
  164.     if(OPEN(i))
  165.       {
  166.     if(equalfiles(name,FILENAME(i)))
  167.       {
  168.         getwtitle(WINDOW(i),wtitle);
  169.             menuitem = findmenuitem(Menu_Window,3,1024,wtitle);
  170.  
  171.             /* Reset the window title */
  172.         strcpy(wtitle,name);
  173.             if(!simpletitle)
  174.           {
  175.             sprintf(num,":%d",VERSION(i)+1);
  176.             strcat(wtitle,num);
  177.               }
  178.  
  179.         setwtitle(WINDOW(i),wtitle);
  180.         setitem(Menu_Window,menuitem,wtitle);
  181.       }
  182.       }
  183. }
  184.  
  185.  
  186.  
  187. /****************************************************************************
  188.  
  189.     Closing and disposing of windows.
  190.     
  191. ****************************************************************************/
  192.  
  193.  
  194. /*
  195.     Close a window.
  196.     Return FALSE if cancelled, otherwise TRUE.
  197. */
  198.  
  199.  
  200. Boolean closethewindow(w,s)
  201. int w;
  202. char *s;
  203. {
  204.   Boolean cancelled = FALSE;
  205.  
  206.   if(isDocumentWindow(w))
  207.     {
  208.       if(savemethod != kAENo && CHANGED(w))
  209.         {
  210.           if((savemethod == kAEAskUser && (cancelled = shouldsavedialog(w,s)) == OK))
  211.             {
  212.           SetCursor(*watchcurs);
  213.               if(!save(w))
  214.             return(FALSE);
  215.         }
  216.           else if (cancelled == CANCEL)
  217.             return(FALSE);
  218.     }
  219.       CloseAWindow(w);
  220.     }
  221.   else if(isScrapWindow(w) && scrapVisible)
  222.     showhideclipboard();
  223.  
  224.   return(TRUE);
  225. }
  226.  
  227.  
  228. /*
  229.     Close all windows.
  230.     Return FALSE if cancelled, otherwise TRUE.
  231. */
  232.  
  233. Boolean closeallwindows()
  234. {
  235.   int i;
  236.   
  237.   SetCursor(*watchcurs);
  238.  
  239.   for(i = 0; i < NUM_WINDOWS; ++i)
  240.     if(OPEN(i))
  241.       {
  242.         if(!closethewindow(i,"quitting"))
  243.         return(FALSE);
  244.         SetCursor(*watchcurs);
  245.       }
  246.  
  247.   return(TRUE);
  248. }
  249.  
  250.  
  251.  
  252. /*
  253.     Close a window.
  254.   
  255.     Reset versions and delete it from the window menu if necessary.
  256. */
  257.  
  258. static char name[256];
  259.  
  260. CloseAWindow(windex)
  261. int        windex;
  262. {
  263.   if (isLegalWindow(windex) && OPEN(windex))
  264.     {
  265.       /* Be careful when closing iconic windows! */
  266.       WindowPtr  whichWindow = iconic(windex)?ICONWINDOW(windex):WINDOW(windex);
  267.       short menuitem, mitems;
  268.  
  269.       /* Remove the window from the window menu if necessary */
  270.       getwtitle(WINDOW(windex),name);
  271.       menuitem = findmenuitem(Menu_Window,3,1024,name);
  272.       if(menuitem >= 3 && menuitem <= 1027)
  273.         {
  274.       char itemstring[256];
  275.       char keyequiv[3];
  276.           DelMenuItem(Menu_Window,menuitem);
  277.       mitems = CountMItems(Menu_Window);
  278.       while(menuitem < 10+4 && menuitem < mitems+1)
  279.         {
  280.           sprintf(itemstring,"/%c",menuitem+'0'-4);
  281.           getitem(Menu_Window,menuitem,itemstring+2);
  282.               DelMenuItem(Menu_Window,menuitem);
  283.           insmenuitem(Menu_Window,itemstring,menuitem-1);
  284.           ++menuitem;
  285.         }
  286.     }
  287.     
  288.       /* 
  289.          Disposing the window causes the window to be deactivated
  290.          and another window to be activated.  To avoid referencing
  291.      a dead window in the deactivate we first hide the window,
  292.      then handle the activate/deactivate explicitly.
  293.      
  294.      The Mac interface model ensures no other activate/deactivate will 
  295.      intervene -- if one was outstanding, it would have been presented
  296.      before this close event.  To be certain, we flush all activate
  297.      events, anyway.
  298.       */
  299.       
  300.       FlushEvents(activMask,0);
  301.  
  302.       /*
  303.          Now we hide the window, which will generate two activate events:
  304.          first a deactivate for the window being hidden, 
  305.      then an activate for the window being brought to the front.
  306.       */
  307.      
  308.       HideWindow(whichWindow);
  309.       
  310.       thefrontwindow = ILLEGAL_WINDOW;
  311.       
  312.       /* Mark the window as closed */
  313.       closed(windex);
  314.  
  315.       if (EventAvail(activMask, &myEvent))
  316.         {
  317.       if (myEvent.what == activateEvt && (WindowPtr)myEvent.message == whichWindow)
  318.         (void) GetNextEvent(activMask, &myEvent);
  319.  
  320.         SetMenus(FALSE,windex);
  321.     }
  322.  
  323.  
  324.       if(FILENAME(windex) != NIL)
  325.      resetversions(FILENAME(windex),ILLEGAL_WINDOW);
  326.  
  327.       if(FILENAME(windex)!=NIL)
  328.         {
  329.           free(FILENAME(windex));
  330.           FILENAME(windex)=NIL;
  331.     }
  332.  
  333.       if(TEHANDLE(windex)!=NIL)
  334.         {
  335.           TEDispose(TEHANDLE(windex));
  336.           TEHANDLE(windex) = NIL;
  337.     }
  338.  
  339.       /* Forget the main window */
  340.       SetWRefCon(WINDOW(windex),0);
  341.       DisposeWindow(WINDOW(windex));
  342.  
  343.       /* and do the same for the icon window */
  344.       if(ICONWINDOW(windex)!=NIL)
  345.         {
  346.           SetWRefCon(ICONWINDOW(windex),0);
  347.       /* Also disposes some necessary controls?  KH */